/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import com.edi.runtime.Event.*;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ICollectionView;
	import mx.collections.ListCollectionView;
	import mx.controls.ComboBox;
	import mx.events.ListEvent;
	
	/**
	 * 下拉组件
	 * 在MXML文件中使用：
	 * <component:EDIComboBox x="185" fieldLength="10"  labelField="caption"  height="22"  width="275"  fieldType="int"  id="sendCbx"  primaryKey="false"  visible="true"  y="55"> 
	 * 
	 */	
	public class EDIComboBox extends ComboBox implements IEDIComponent,IEDIDataLoad
	{
		private var _dataField:String;
		private var _field:String;
		private var _fieldtype:String;
		private var _fieldLength:int;
		private var _table:String;
		private var _primaryKey:String="";
		private var _dataSourceName:String="";
		public var edidefaultField:String=null;
		public var edidataXmlList:XMLList=null;
		/**
		 * 构造 combobox对象
		 * 
		 */		
		public function EDIComboBox()
		{
			super();
			addEventListener(ListEvent.CHANGE,changeHandle);
			
		}
		
		/**
		 *  ListEvent.CHANGE 事件 监听数据发生变化后，通知所绑定的数据集。
		 * @param event
		 * 
		 */		
		private function changeHandle(event:ListEvent):void
		{
			if(this.parentApplication is EDIApplication && _dataSourceName!="")
			{
				var ede:EDIDataEvent=new EDIDataEvent(EDIDataEvent.Data_RollingBefore,true); 
				ede.tableName=_dataSourceName;				
				ede.ediTarget=this;
				ede.index=selectedIndex;
				dispatchEvent(ede);
			}
		}
		
		/**
		 * 实现IEDIComponent接口的属性，设置表名。
		 * @param value
		 * 
		 */	 
		public function set table(value:String):void
		{
			_table=value;
			EDIEventProxy.listenerDataRolling(value,this);
		}
		public function get table():String
		{return _table;}
		
		////////
		public function setDefault(myname:String):void{
			
			if(myname != null && myname !=""){
				
				var lc:ListCollectionView=(dataProvider as ListCollectionView);
			for(var i:int=0;i<lc.length;i++){
				if(lc[i][this.labelField] == myname){
					this.selectedIndex=i;
					break;
				}
			}
			}else{
				EDIApplication.outputDebug("edidefaultField is null");
			}
		}
		////////
		
		/**
		 *  实现IEDIComponent接口的属性，设置数据源名称。
		 * @param value
		 * 
		 */		
		public function set dataSourceName(value:String):void
		{
			_dataSourceName=value;
			EDIEventProxy.listenerDataLoad(value,this);
		}
		
		public function get dataSourceName():String
		{return _dataSourceName;}
		
		/**
		 *  实现IEDIComponent接口的属性，设置是否是主键
		 * @param value
		 * 
		 */		
		public function set primaryKey(value:String):void
		{_primaryKey=value;}
		public function get primaryKey():String
		{return _primaryKey;}
		
		/**
		 *  实现IEDIComponent接口的属性，设置字段名
		 * @param value
		 * 
		 */		
		public function set field(value:String):void
		{_field=value;}
		public function get field():String
		{return _field;}
		
		/**
		 *  实现IEDIComponent接口的属性，设置字段类型
		 * @param value
		 * 
		 */		
		public function set fieldType(value:String):void
		{_fieldtype=value;}
		public function get fieldType():String
		{return _fieldtype;}
		
		/**
		 * 实现IEDIComponent接口的属性，设置字段长度
		 * @param value
		 * 
		 */		
		public function set fieldLength(value:int):void{_fieldLength=value;}
		public function get fieldLength():int{return _fieldLength;}				
		
		/**
		 * 设置对应的真正值的字段名 
		 * @param value
		 * 
		 */		
		public function set dataField(value:String):void
		{_dataField=value;}
		public function get dataField():String
		{return _dataField;}
		
		/**
		 * 获得需所选本行的dataField对应的值
		 * @return 
		 * 
		 */		
		public function get selectItemValue():String
		{
			return dataProvider is EDIDataTable?dataProvider[selectedIndex][_dataField]:"";
			return "";
		}
		
		/**
		 * 实现IEDIComponent接口的属性，捕获数据滚动
		 * @param event
		 * 
		 */				
		public function dataRollingHandler(event:EDIDataEvent):void
		{
			if((dataProvider as ICollectionView).length!=0)
			{
				var i:int;
				var edt:EDIDataTable=dataProvider as EDIDataTable;
				if(edt!=null)
				{
					for(i=0;i<edt.length;i++)
					{
						if(event.value[field].toString()==edt[i][dataField].toString())
						{
							selectedIndex=i;
							break;
						}
					}
				}
			}
		}
		
		/**
		 * 定位到 dataField的值与value相同的位置
		 * @param value
		 * @param dataField
		 * 
		 */		
		public function showByDataValue(value:String,dataField:String=""):void
		{
			if(dataField==""){dataField=this.dataField}
			var lc:ListCollectionView=(dataProvider as ListCollectionView);
			for(var i:int=0;i<lc.length;i++)
			{
				if(lc[i][dataField]==value)
				{
					this.selectedIndex=i;
					break;
				}
			}
		}
		
		/**
		 *  实现IEDIDataLoad接口的属性，实现数据的加载 
		 * @param event
		 * 
		 */		
		public function dataLoadHandler(event:EDIDataEvent):void
		{
			dataProvider=event.value;
			
			
		}
		
		/**
		 * 返回edi所需要的值 
		 * @return 
		 * 
		 */		
		public function get ediValue():String
		{
			return selectItemValue;
		}
	}
}